package com.ruoyi.controller.archives;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.domain.archives.PInfo;
import com.ruoyi.service.archives.PInfoService;
import com.ruoyi.utils.MinIoUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author lz
 * @Version 1.0
 * @Company 滁州大健康与养老产业研究院
 * @ClassName PInfoController
 * @Description TODO
 * @Date 2022/05/28 8:41
 */
@RestController
@RequestMapping("/archives/dpig")
public class PInfoController extends BaseController {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private PInfoService infoService;
    @Resource
    private MinIoUtil minIoUtil;
    @Value("${minio.bucketName}")
    private String bucketName;

    /**
     *  分页模糊查询所有猪的信息
     * @param pInfo 猪信息
     * @return 所有猪的信息
     */
    @PreAuthorize("@ss.hasPermi('archives:dpig:query')")
    @GetMapping
    public TableDataInfo getAllInfo(PInfo pInfo) {
        startPage();
        List<PInfo> pInfoList = infoService.getAllInfo(pInfo);
        return getDataTable(pInfoList);
    }


    /**
     * 添加猪的信息
     * @param pInfo 猪的信息
     * @return 受影响行数
     */
    @PreAuthorize("@ss.hasPermi('archives:dpig:add')")
    @PostMapping("/createInfo")
    public AjaxResult addPigInfo(@RequestBody PInfo pInfo) {

        int row = infoService.addPigInfo(pInfo);

        if (row == 501) {
            return AjaxResult.error("猪儿标已经存在，无法重复添加");
        }

        if (row <= 0) {
            return AjaxResult.error("添加失败！");
        }
        return AjaxResult.success("添加成功！");
    }

    /**
     * 根据id删除猪的信息
     * @param pigId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('archives:dpig:remove')")
    @DeleteMapping("/{pigId}")
    public AjaxResult removePigInfoById(@PathVariable Long[] pigId) {
        int row = infoService.removePigInfoById(pigId);

        if (row > 0) {
            return AjaxResult.success("删除成功！");
        }
        return AjaxResult.error("删除失败！");
    }

    /**
     * 更新猪的信息
     * @param pInfo 猪信息
     * @return 受影响行数
     */
    @PreAuthorize("@ss.hasPermi('archives:dpig:edit')")
    @PutMapping
    public AjaxResult putPingInfo(@RequestBody PInfo pInfo) {
        int row = infoService.putPingInfo(pInfo);

        if (row > 0) {
            return AjaxResult.success("更新成功！");
        }
        return AjaxResult.error("更新失败！");
    }
}
